#include <bits/stdc++.h>

#define fi first
#define se second
#define eb emplace_back
#define ep emplace
#define rep(i, x, y) for(int i = (x); i <= (y); i++)
#define per(i, x, y) for(int i = (x); i >= (y); i--)

using namespace std;

using ll = long long;
using pii = pair < int, int >;
using vec = vector < int >;
using veg = vector < pii >;

const int N = 1e5 + 10;
const int inf = 0x3f3f3f3f;

int n, m;
vec G[N], T[N];
int dis[N];
int fa[N], tfa[21][N], dep[N];

int gf(int x) {
	return fa[x] == x ? x : fa[x] = gf(fa[x]);
}

void dfs(int x, int p) {
	dep[x] = dep[p] + 1;
	for(auto y : G[x]) dfs(y, x);
}

int lca(int x, int y) {
	if(dep[x] < dep[y]) swap(x, y);
	per(i, 20, 0) if(dep[tfa[i][x]] >= dep[y]) x = tfa[i][x];
	if(x == y) return x;
	per(i, 20, 0) if(tfa[i][x] != tfa[i][y]) x = tfa[i][x], y = tfa[i][y];
	return tfa[0][x];
}

int main() {
#ifdef YJR_2333_TEST
	freopen("1.in", "r", stdin);
#endif
	//ios :: sync_with_stdio(false), cin.tie(0), cout.tie(0);
	scanf("%d %d", &n, &m); rep(i, 2, n) scanf("%d", &fa[i]), G[fa[i]].eb(i), tfa[0][i] = fa[i];
	rep(i, 1, n) fa[i] = i; fa[1] = 0; // fa[1]  ok
	dfs(1, 0);
	rep(i, 1, 20) rep(j, 1, n) tfa[i][j] = tfa[i - 1][tfa[i - 1][j]];
	rep(i, 1, m) {
		int u, v; scanf("%d %d", &u, &v);
		T[u].eb(v), T[v].eb(u);
	} rep(i, 1, n) dis[i] = inf; dis[1] = 0; queue < int > q; q.ep(1);
	while(q.size()) {
		int x = q.front(); q.pop();
		auto goit = [&](int x, int p, int d) {
			x = gf(x);
			while(dep[x] >= dep[p]) {
				if(dis[x] > d) {
					dis[x] = d; q.ep(x);
				}
				fa[x] = tfa[0][x];
				x = gf(x);
			}
		};
		for(auto y : T[x]) {
			int z = lca(x, y);
			goit(x, z, dis[x] + 1), goit(y, z, dis[x] + 1);
		}
	} rep(i, 1, n) if(dis[i] > n) dis[i] = -1;
	rep(i, 2, n) printf("%d ", dis[i]); puts("");
}
